import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class MenuServer {
	public static boolean isNonNegativeNumber(String s) {
		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(i) < '0' || s.charAt(i) > '9') {
				return false;
			}
		}
		return true;
	}
	
	
	public static void main(String[] args) {
		try {
			ServerSocket s = new ServerSocket(8189);

			Socket incoming = s.accept();
			try {
				InputStream inStream = incoming.getInputStream();
				OutputStream outStream = incoming.getOutputStream();

				Scanner in = new Scanner(inStream);
				PrintWriter out = new PrintWriter(outStream, true);

				out.println("Hello! Enter quit to exit.");

				boolean done = false;
				while (!done && in.hasNextLine()) {
					String line = in.nextLine();
					if (line.equals("quit")) {
						done = true;
					} else if (line.equals("menu")) {
						out.println("MENU");
						out.println("1.display nowtime");
						out.println("2.display ip");
						out.println("3.search file");
						out.println("4.quit the menu");
						while (in.hasNextLine()) {	
							line = in.nextLine();
							if (isNonNegativeNumber(line)) {
								int num = Integer.parseInt(line);
								if (num == 1) {
									out.println(new SimpleDateFormat("yyyy-MM-dd HH:ss").format(new Date()));
								} else if (num == 2) {
									out.println(incoming.getInetAddress());
								} else if (num == 3) {
									
								} else if (num == 4) {
									break;
								} else {
									out.println("illegal input");
								}
							} else {
								out.println("illegal input");
							}
							out.println("MENU");
							out.println("1.display nowtime");
							out.println("2.display ip");
							out.println("3.search file");
							out.println("4.quit the menu");
						}
					} else {
						out.println("illegal input");
					}
				}
				in.close();
			} finally {
				incoming.close();
			}
			s.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
